﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CustomsSystem.BLL;
using Maticsoft.Common;

namespace WEB.plan
{
    public partial class planView : System.Web.UI.Page
    {
        planManage bll = new planManage();
        DataSet ds = new DataSet();
        protected void Page_Load(object sender, EventArgs e)
        {
            #region 数据载入
            if (!IsPostBack)
            {
                tbTimestart.Text = DateTime.Now.ToString("yyyy/MM/01");
                tbTimestop.Text = DateTime.Now.AddDays(1).ToShortDateString();

            }
            #region 搜索条件

            string strSql = Query();
            if (!strSql.Trim().Equals("1=1"))
            {
                ds = bll.GetView(strSql);
                gvPlan.DataSource = ds;
            }

            else
            {
                ds = bll.GetView("");
                gvPlan.DataSource = ds;
            }
            #endregion
            if (!IsPostBack)
            {
                this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");
                this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");
                gvPlan.DataBind();
            }
            #endregion
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {

            DataSet ds = new DataSet();
            //日期文本的判断
            if (tbTimestart.Text != "" && tbTimestop.Text != "")
            {
                if (Convert.ToDateTime(tbTimestart.Text) >= Convert.ToDateTime(tbTimestop.Text))
                {
                    MessageBox.Show(this, "结束日期不能大于开始日期！");
                    return;
                }
            }
            //进行搜索
            ds = bll.GetView(Query());
            gvPlan.DataSource = ds;
            gvPlan.DataBind();
        }

        /// <summary>
        /// 按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvPlan_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string index = e.CommandArgument.ToString();

            if (e.CommandName == "next")//下推
            {
                int id = Convert.ToInt32(gvPlan.DataKeys[int.Parse(index)].Value);
                Response.Redirect(@"~\sheetInfo\sheetEdit.aspx?Next=" + id);
            }
            else if (e.CommandName == "Edit")//编辑
            {
                int id = Convert.ToInt32(gvPlan.DataKeys[int.Parse(index)].Value);
                Response.Redirect("planEdit.aspx?ID=" + id);
            }
            else if (e.CommandName == "Look")//查看
            {
                int id = Convert.ToInt32(gvPlan.DataKeys[int.Parse(index)].Value);
                Response.Redirect(@"\Client\sheetDetails1.aspx?plan=" + id);
            }

        }
        /// <summary>
        /// 换页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvPlan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPlan.PageIndex = e.NewPageIndex;
            gvPlan.DataBind();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <returns></returns>
        public string Query()
        {
            string strQuery = ddlQuery.SelectedValue;
            string strSql = " 1=1 ";
            if (tbQuery.Text.Trim() != "")
            {
                switch (ddlQuery.SelectedIndex)  //根据选项传不同的SQL语句
                {
                    case 1:
                    case 0:
                    case 2:
                        strSql += string.Format("and {0} like '%{1}%'", strQuery, tbQuery.Text.Trim());
                        break;
                    default: if (!PageValidate.IsNumber(tbQuery.Text.Trim())) tbQuery.Text = "0";
                        strSql += string.Format(" and {0} = '{1}'", strQuery, tbQuery.Text.Trim());
                        break;
                }
            }
            //日期搜索的判断
            if (tbTimestart.Text != "")
            {
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    strSql = strSql + " and inputDate between '" + tbTimestart.Text.Trim() + "' and '" + timeStop + "'";
                }
                else
                {
                    strSql = strSql + " and inputDate between '" + tbTimestart.Text.Trim() + "' and '" + tbTimestop.Text.Trim() + "'";
                }
            }
            strSql += " order by planID  " + ddlasc.SelectedValue;
            return strSql;
        }

        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Del_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(((Button)sender).CommandArgument.ToString());

            if (bll.Delete(id))
            {
                MessageBox.ShowAndRedirect(this, "删除成功", "planView.aspx");
            }
            else
            {
                MessageBox.Show(this, "删除失败");
            }

        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlasc_SelectedIndexChanged(object sender, EventArgs e)
        {
            gvPlan.DataBind();
        }
    }
}